/*
 * For modifications by Bob Breznak
 * 
 * Copyright (c) 2006, Bob Breznak
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *    * Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright 2006 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.iisageek.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.iisageek.client.dialogs.AboutDialog;
import com.iisageek.client.dialogs.LoadingDialog;
import com.iisageek.client.widgets.xml.Node;
import com.iisageek.client.widgets.xml.Server;
import com.iisageek.client.widgets.xml.XMLHandler;

/**
 * The main entry point for the application.  
 * There will be only one instance of lnl. It is a singleton.
 * 
 * Singleton.
 * 
 * @author Bob Breznak
 */
public class lnl  implements XMLHandler, EntryPoint, WindowResizeListener {

	// Allows for only one instance of lnl
	private static lnl singleton;
	public final Configure config = new Configure();
	
	private DockPanel masterpanel = new DockPanel();
	private TopPanel  headerpanel = new TopPanel();
	private MainPanel mainpanel = new MainPanel();
	private Shortcuts shortcutpanel = new Shortcuts();
	
	protected MembersPanel memberspanel = new MembersPanel();
	protected WorkordersPanel workorderspanel = new WorkordersPanel();
	
	protected LoadingDialog loadingDlg = new LoadingDialog();


	/**
	 * onModuleLoad
	 * 
	 * The entry point into the entire system. This shows the loading dialog
	 * and then sends a post to the server for the user's login credentals.
	 */
	public void onModuleLoad() {
		showLoading();
		Server.requestData(Configure.getServer() + "/db.cgi?rm=login", this);
	}
			
	/**
	 * onWindowResized
	 * 
	 * This method is executed with the user resizes the window. The panels are 
	 * adjusted to keep the proper preportions
	 * 
	 * @param width The width of the window
	 * @param height The height of the window
	 */
	public void onWindowResized(int width, int height) {

		int shortcutHeight = height - shortcutpanel.getAbsoluteTop() - 8;
		if (shortcutHeight < 1) {
			shortcutHeight = 1;
		}
		shortcutpanel.setHeight("" + shortcutHeight);
	}
		
	/** 
	 * showLoading
	 * 
	 * Displays a loading dialog in the center of the screen
	 *
	 */
	public void showLoading() {
		loadingDlg.show();
	}

	/** 
	 * hideLoading
	 * 
	 * Hides the loading dialog in the center of the screen
	 *
	 */
	public void hideLoading() {
		loadingDlg.hide();
	}
	
	/**
	 * showError
	 * 
	 * Changes the body of the site to "Error." and produces an error dialog
	 */
	public void showError() {
		MainPanel.get().setMode(MainPanel.TEXT, "Error.");
		new AboutDialog("System Error","There has been an error.");
	}
	
	/**
	 * get
	 * 
	 * Returns the single instance of lnl
	 * 
	 * @return lnl
	 */
	public static lnl get() {
		return singleton;
	}

	/**
	 * receivedData
	 * 
	 * Invoked from a HTTP call. 
	 * 
	 * @param root (Node) the root XML node returned from the HTTP call  
	 */
	public void receivedData(Node root) {

		singleton = this;
		
        Member member = new Member(root.getChild(0));
        
        if(member.isOfficer()) {
        	headerpanel.setUsername(member.getFirstName() + " " + member.getLastName());
		
			headerpanel.setWidth("100%");
	
			masterpanel.add(headerpanel, DockPanel.NORTH);
			masterpanel.add(shortcutpanel, DockPanel.WEST);
			masterpanel.add(mainpanel, DockPanel.CENTER);
			masterpanel.setWidth("100%");
	
			masterpanel.setSpacing(4);
			masterpanel.setCellWidth(mainpanel, "100%");
	
			Window.addWindowResizeListener(this);
	
			Window.enableScrolling(true);
			Window.setMargin("0px");
	
			masterpanel.setWidth("100%");
			RootPanel.get().add(masterpanel);

        } else {
        	lnl.get().showError();
        }
        
		onWindowResized(Window.getClientWidth() - 50,
				Window.getClientHeight() - 10);
		hideLoading();
	}
}
